#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define all(x) (x).begin(),(x).end()
typedef long long ll;

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<VI> VVI;

typedef pair<int,int> PII;
typedef vector<PII> VPII;
#define X first
#define Y second
#define mp make_pair

#define two(x) (1<<(x))
#define twoll(x) ((long long)1<<(x))
#define contain(s,x) (((s)&two(x))!=0)
#define containll(s,x) (((s)&twoll(x))!=0)

#define db(a) cout << #a << "=" << a << " "
#define dbn cout << endl
#define sqr(x) (x)*(x)

string STR(int n) { char tmp[20]; sprintf(tmp,"%d",n); return tmp; }
int INT(string s) { return atoi(s.c_str()); }
template <class Ty, class Tx> Ty to(const Tx &x) { Ty y; stringstream ss; ss<<x; ss>>y; return y; }

const int INF = 1000000007;
const double EPS = 1e-10;

char buf[1<<20];
int TC;

template <class T>
struct DiGraph {
	T INF;
	struct Edge {
		int target, weight;
		Edge(int t, int w) : target(t), weight(w) { }
	};
	vector<vector<Edge> > adj;
	
	DiGraph(int V) { adj.resize(V); INF = 1000000007; }
	int getV() const { return adj.size(); }
	int addVertex() {
		adj.push_back(vector<Edge>());
		return adj.size()-1;
	}
	void addEdge(int a, int b, int c) {
		adj[a].push_back(Edge(b,c));
	}
	int getW(int a, int b) {
		for (int i = 0; i < adj[a].size(); ++i)
			if (adj[a][i].target == b)
				return adj[a][i].weight;
	}
	bool editWeight(int a, int b, int c) {
		for (int i = 0; i < adj[a].size(); ++i)
			if (adj[a][i].target == b)
			{
				adj[a][i].weight = c;
				return true;
			}
		return false;
	}

	void Dijkstra(int start, vector<T>& dist, vector<int>& par) const {
		int V = getV();
		dist.resize(V);
		par.resize(V);
		for (int i = 0; i < V; ++i)
			dist[i] = INF, par[i] = -1;
		priority_queue<pair<T,pair<int,int> >, vector<pair<T,pair<int,int> > >, greater<pair<T,pair<int,int> > > > pq;
		pq.push(make_pair(0,make_pair(start,-1)));
		while (!pq.empty())
		{
			T curDist = pq.top().first;
			int curNode = pq.top().second.first;
			int curPar = pq.top().second.second;
			pq.pop();
			if (curDist >= dist[curNode])
				continue;
			dist[curNode] = curDist; par[curNode] = curPar;
			for (int i = 0; i < adj[curNode].size(); ++i) {
				int nextNode = adj[curNode][i].target;
				T nextDist = adj[curNode][i].weight;
				const T newDist = curDist + nextDist;
				if (newDist < dist[nextNode])
					pq.push(make_pair(newDist,make_pair(nextNode,curNode)));
			}
		}
	}
};

int N,M;

int main() {
#define SUBMIT 0
#if !SUBMIT
	freopen("DATA.in", "rt", stdin);
	freopen("DATA.ou", "wt", stdout);
#endif

	gets(buf); TC = atoi(buf);
	
	while (TC--) {
		scanf("%d%d",&N,&M);
		DiGraph<int> dg(N);
		for (int i = 0; i < M; ++i) {
			int c,d,w;
			scanf("%d%d%d",&c,&d,&w);
			dg.addEdge(c,d,w);
			dg.addEdge(d,c,w);
		}
		VI path;
		int k;
		scanf("%d",&k);
		for (int i = 0; i < k; ++i) {
			int t; scanf("%d",&t); path.push_back(t);
		}
		int res = 0;
		for (int i = 0; i < k-1; ++i)
			res += dg.getW(path[i],path[i+1]);
		for (int i = 0; i < k-1; ++i) {
			int cur = 0;
			for (int j = 0; j < i; ++j)
				cur += dg.getW(path[j],path[j+1]);
			int tmp = dg.getW(path[i],path[i+1]);
			dg.editWeight(path[i],path[i+1],INF);
			VI dist,par;
			dg.Dijkstra(path[i],dist,par);
			cur += dist[path.back()];
			dg.editWeight(path[i],path[i+1],tmp);
			res = max(res,cur);
		}
		if (res >= INF)
			printf("-1\n");
		else
			printf("%d\n",res);
			
	}

	return 0;
}





